package com.tomecode.tools.maven.settings.gui.frm;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

import com.tomecode.tools.maven.settings.gui.images.ImageFactory;
import com.tomecode.tools.maven.settings.gui.utils.GuiUtils;
import org.eclipse.swt.layout.grouplayout.GroupLayout;
import org.eclipse.swt.layout.grouplayout.LayoutStyle;

/**
 * Dialog for selecting plugin groups
 * 
 * @author Tomas Frastia
 * @see http://www.tomecode.com or
 *      http://code.google.com/p/maven-settings-editor/
 * 
 */
public final class FrmSelectPluginGroups extends Dialog {

	private String result;
	private Shell shlSelectPluginGroup;
	private List listPluginGroups;
	private java.util.List<String> existsPluginGroups;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FrmSelectPluginGroups(Shell parent, java.util.List<String> existsPluginGroups) {
		super(parent, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		setText("Pluging Groups");
		this.existsPluginGroups = existsPluginGroups;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final String open(java.util.List<String> newPluginGroups) {
		createContents(newPluginGroups);
		shlSelectPluginGroup.open();
		shlSelectPluginGroup.layout();
		Display display = getParent().getDisplay();
		while (!shlSelectPluginGroup.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents(java.util.List<String> newPluginGroups) {
		shlSelectPluginGroup = new Shell(getParent(), getStyle());
		shlSelectPluginGroup.setImage(ImageFactory.MAVEN_SETTINGS_EDITOR_16);
		shlSelectPluginGroup.setSize(459, 324);
		shlSelectPluginGroup.setText("Select Plugin Group");

		Composite composite = new Composite(shlSelectPluginGroup, SWT.NONE);
		if (GuiUtils.isWindows()) {
			composite.setBounds(0, 0, 444, 275);
		}

		Button btnCancel = new Button(composite, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result = null;
				shlSelectPluginGroup.close();
			}
		});
		if (GuiUtils.isWindows()) {
			btnCancel.setBounds(340, 238, 94, 30);
		}
		btnCancel.setText("Cancel");

		final Button btnSelect = new Button(composite, SWT.NONE);
		btnSelect.setEnabled(false);
		btnSelect.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				result = listPluginGroups.getItem(listPluginGroups.getSelectionIndex());
				shlSelectPluginGroup.close();
			}
		});
		if (GuiUtils.isWindows()) {
			btnSelect.setBounds(240, 238, 94, 30);
		}
		btnSelect.setText("Select");

		Group group = new Group(composite, SWT.NONE);
		if (GuiUtils.isWindows()) {
			group.setBounds(0, 0, 444, 234);
		}
		group.setLayout(null);

		listPluginGroups = new List(group, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		listPluginGroups.setBounds(10, 10, 433, 222);
		GroupLayout gl_shlSelectPluginGroup = new GroupLayout(shlSelectPluginGroup);
		gl_shlSelectPluginGroup.setHorizontalGroup(gl_shlSelectPluginGroup.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE));
		gl_shlSelectPluginGroup.setVerticalGroup(gl_shlSelectPluginGroup.createParallelGroup(GroupLayout.LEADING).add(composite, GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE));
		GroupLayout gl_composite = new GroupLayout(composite);
		gl_composite.setHorizontalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING)
				.add(gl_composite.createSequentialGroup().add(251).add(btnSelect, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.RELATED).add(btnCancel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE).addContainerGap())
				.add(group, GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE));
		gl_composite.setVerticalGroup(gl_composite.createParallelGroup(GroupLayout.LEADING).add(
				gl_composite.createSequentialGroup().add(group, GroupLayout.PREFERRED_SIZE, 242, GroupLayout.PREFERRED_SIZE).add(18)
						.add(gl_composite.createParallelGroup(GroupLayout.BASELINE).add(btnCancel, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE).add(btnSelect, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))));
		composite.setLayout(gl_composite);
		shlSelectPluginGroup.setLayout(gl_shlSelectPluginGroup);
		if (GuiUtils.isWindows()) {
			listPluginGroups.setBounds(10, 10, 426, 218);
		}
		listPluginGroups.addMouseListener(new MouseAdapter() {
			public final void mouseDoubleClick(MouseEvent e) {
				int index = listPluginGroups.getSelectionIndex();
				if (index != -1) {
					result = listPluginGroups.getItem(index);
					shlSelectPluginGroup.close();
				}
			}
		});
		listPluginGroups.addSelectionListener(new SelectionAdapter() {
			@Override
			public final void widgetSelected(SelectionEvent e) {
				btnSelect.setEnabled(listPluginGroups.getSelectionIndex() != -1);
			}
		});

		fillList(newPluginGroups);

	}

	private final void fillList(java.util.List<String> newPluginGroups) {
		for (String pluginGroup : newPluginGroups) {
			if (!existsPluginGroups.contains(pluginGroup)) {
				listPluginGroups.add(pluginGroup);
			}
		}
	}
}
